/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 18.10.2011., 21:12:36
 */
package hr.fer.zpr.nmbp.project1.presentation;

import hr.fer.zpr.nmbp.project1.bll.BolestBLL;
import hr.fer.zpr.nmbp.project1.bll.BolnicaBLL;
import hr.fer.zpr.nmbp.project1.bll.DijagnozaBLL;
import hr.fer.zpr.nmbp.project1.bll.LijecnikBLL;
import hr.fer.zpr.nmbp.project1.bll.PacijentBLL;
import hr.fer.zpr.nmbp.project1.dao.DAOFactory;
import hr.fer.zpr.nmbp.project1.model.Bolest;
import hr.fer.zpr.nmbp.project1.model.Bolnica;
import hr.fer.zpr.nmbp.project1.model.Dijagnoza;
import hr.fer.zpr.nmbp.project1.model.Lijecnik;
import hr.fer.zpr.nmbp.project1.model.Pacijent;
import hr.fer.zpr.nmbp.project1.presentation.util.DisplayUtil;
import hr.fer.zpr.nmbp.project1.presentation.util.MyListModel;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXImagePanel.Style;

/**
 *
 * @author Miki
 */
public class MainFrame extends javax.swing.JFrame {

    /** Creates new form MainFrame */
    public MainFrame() {
        beforeInit();
        initComponents();
        afterInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel6 = new javax.swing.JPanel();
        jXImagePanel1 = new org.jdesktop.swingx.JXImagePanel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jList5 = new javax.swing.JList();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("eBolnica");

        javax.swing.GroupLayout jXImagePanel1Layout = new javax.swing.GroupLayout(jXImagePanel1);
        jXImagePanel1.setLayout(jXImagePanel1Layout);
        jXImagePanel1Layout.setHorizontalGroup(
            jXImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 314, Short.MAX_VALUE)
        );
        jXImagePanel1Layout.setVerticalGroup(
            jXImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 261, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Home", jPanel6);

        jButton1.setText("Dodaj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList1);

        jButton6.setText("Prikaži");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Obriši");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton6, jButton7});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Bolnica", jPanel1);

        jButton2.setText("Dodaj");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jList2);

        jButton8.setText("Prikaži");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Obriši");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton9)))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton8, jButton9});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton8)
                    .addComponent(jButton9))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Lijecnik", jPanel2);

        jButton4.setText("Dodaj");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(jList3);

        jButton10.setText("Prikaži");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setText("Obriši");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton11)))
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton10, jButton11, jButton4});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton10)
                    .addComponent(jButton11))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Pacijent", jPanel4);

        jButton5.setText("Dodaj");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jList4.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList4.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(jList4);

        jButton12.setText("Prikaži");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jButton13.setText("Obriši");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton16.setText("Pretraži");
        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton16ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton16)))
                .addContainerGap())
        );

        jPanel5Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton12, jButton13, jButton16, jButton5});

        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton12)
                    .addComponent(jButton13)
                    .addComponent(jButton16))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Dijagnoza", jPanel5);

        jButton3.setText("Dodaj");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jList5.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList5.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane5.setViewportView(jList5);

        jButton14.setText("Prikaži");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton15.setText("Obriši");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton15)))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton14, jButton15, jButton3});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton14)
                    .addComponent(jButton15))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Bolest", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        actionBolnicaDodaj();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        actionLijecnikDodaj();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        actionBolestDodaj();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        actionPacijentDodaj();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        actionDijagnozaDodaj();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        actionBolnicaPrikazi();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        actionLijecnikPrikazi();
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        actionPacijentPrikazi();
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        actionDijagnozaPrikazi();
    }//GEN-LAST:event_jButton12ActionPerformed

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
        actionBolestPrikazi();
    }//GEN-LAST:event_jButton14ActionPerformed

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
        actionBolestObrisi();
    }//GEN-LAST:event_jButton15ActionPerformed

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
        actionDijagnozaObrisi();
    }//GEN-LAST:event_jButton13ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        actionPacijentObrisi();
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        actionBolnicaObrisi();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        actionLijecnikObrisi();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton16ActionPerformed
        actionDijagnozaPretrazi();
    }//GEN-LAST:event_jButton16ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JList jList4;
    private javax.swing.JList jList5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXImagePanel jXImagePanel1;
    // End of variables declaration//GEN-END:variables

    
    private BolnicaNewPanel bolnicaNewPanel = new BolnicaNewPanel();
    private LijecnikNewPanel lijecnikNewPanel = new LijecnikNewPanel();
    private BolestNewPanel bolestNewPanel = new BolestNewPanel();
    private PacijentNewPanel pacijentNewPanel = new PacijentNewPanel();
    private DijagnozaNewPanel dijagnozaNewPanel = new DijagnozaNewPanel();
    
    private DijagnozaSearchPanel dijagnozaSearchPanel = new DijagnozaSearchPanel();
    
    private BolnicaBLL bolnicaBLL;
    private LijecnikBLL lijecnikBLL;
    private BolestBLL bolestBLL;
    private PacijentBLL pacijentBLL;
    private DijagnozaBLL dijagnozaBLL;
    
    private MyListModel<Bolnica> bolnicaModel = new MyListModel<Bolnica>();
    private MyListModel<Lijecnik> lijecnikModel = new MyListModel<Lijecnik>();
    private MyListModel<Pacijent> pacijentModel = new MyListModel<Pacijent>();
    private MyListModel<Dijagnoza> dijagnozaModel = new MyListModel<Dijagnoza>();
    private MyListModel<Bolest> bolestModel = new MyListModel<Bolest>();
    
    private void beforeInit() {
        getContentPane().setBackground(UIManager.getColor("Panel.background"));
        DisplayUtil.SystemLookAndFeel();
    }
    
    private void afterInit() {
        DAOFactory.init();
        bolnicaBLL = new BolnicaBLL();
        lijecnikBLL = new LijecnikBLL();
        bolestBLL = new BolestBLL();
        pacijentBLL = new PacijentBLL();
        dijagnozaBLL = new DijagnozaBLL();
        
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                DAOFactory.close();
            }
        });
        
        initLists();
        
        dijagnozaSearchPanel.setDijagnozaBLL(dijagnozaBLL);
       
        jXImagePanel1.setStyle(Style.SCALED_KEEP_ASPECT_RATIO);
        jXImagePanel1.setImage(new ImageIcon(MainFrame.class.getResource("/hr/fer/zpr/nmbp/project1/presentation/res/screen.jpg")).getImage());
        jXImagePanel1.setOpaque(true);
        
        pack();
        setMinimumSize(getSize());
        setLocationRelativeTo(null);
        
        setIconImage(new ImageIcon(MainFrame.class.getResource("/hr/fer/zpr/nmbp/project1/presentation/res/hospital.png")).getImage());
    }
    
    
    private void initLists() {
        jList1.setModel(bolnicaModel);
        jList2.setModel(lijecnikModel);
        jList3.setModel(pacijentModel);
        jList4.setModel(dijagnozaModel);
        jList5.setModel(bolestModel);
        loadAll();
        
        jList1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2) return;
                actionBolnicaPrikazi();
            }
        });
        jList1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) return;
                actionBolnicaPrikazi();
            }
        });
        
        jList2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2) return;
                actionLijecnikPrikazi();
            }
        });
        jList2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) return;
                actionLijecnikPrikazi();
            }
        });
        
        jList3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2) return;
                actionPacijentPrikazi();
            }
        });
        jList3.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) return;
                actionPacijentPrikazi();
            }
        });
        
        jList4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2) return;
                actionDijagnozaPrikazi();
            }
        });
        jList4.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) return;
                actionDijagnozaPrikazi();
            }
        });
        
        jList5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2) return;
                actionBolestPrikazi();
            }
        });
        jList5.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) return;
                actionBolestPrikazi();
            }
        });
    }
    
    private void prikaziGresku(String msg) {
        JOptionPane.showMessageDialog(this, msg, "Greška", JOptionPane.WARNING_MESSAGE);
    }
    
    private int prikaziSaveCancle(String title, JPanel panel) {
        Object[] options = {"Save", "Cancel"};
        return JOptionPane.showOptionDialog(
					this,
					panel,
					title,
					JOptionPane.YES_NO_OPTION,
					JOptionPane.PLAIN_MESSAGE,
					null,
					options,
					options[0]);
    }
    
    private int prikaziOk(String title, JPanel panel) {
        Object[] options = {"Ok"};
        return JOptionPane.showOptionDialog(
					this,
					panel,
					title,
					JOptionPane.OK_OPTION,
					JOptionPane.PLAIN_MESSAGE,
					null,
					options,
					options[0]);
    }
    
    private void loadAll() {
        bolnicaModel.setList(bolnicaBLL.loadAll());
        lijecnikModel.setList(lijecnikBLL.loadAll());
        pacijentModel.setList(pacijentBLL.loadAll());
        dijagnozaModel.setList(dijagnozaBLL.loadAll());
        bolestModel.setList(bolestBLL.loadAll());
    }
    
    private void actionBolnicaObrisi() {
        Bolnica bolnica = (Bolnica) jList1.getSelectedValue();
        if (bolnica == null) return;
        bolnicaBLL.delete(bolnica);
        loadAll();
    }
    
    private void actionLijecnikObrisi() {
        Lijecnik lijecnik = (Lijecnik) jList2.getSelectedValue();
        if (lijecnik == null) return;
        lijecnikBLL.delete(lijecnik);
        loadAll();
    }
    
    private void actionPacijentObrisi() {
        Pacijent pacijent = (Pacijent) jList3.getSelectedValue();
        if (pacijent == null) return;
        pacijentBLL.delete(pacijent);
        loadAll();
    }
    
    private void actionDijagnozaObrisi() {
        Dijagnoza dijagnoza = (Dijagnoza) jList4.getSelectedValue();
        if (dijagnoza == null) return;
        dijagnozaBLL.delete(dijagnoza);
        loadAll();
    }
    
    private void actionBolestObrisi() {
        Bolest bolest = (Bolest) jList5.getSelectedValue();
        if (bolest == null) return;
        bolestBLL.delete(bolest);
        loadAll();
    }
    
    private void actionBolnicaDodaj() {
        bolnicaNewPanel.reset();
        while (true) {
            int n = prikaziSaveCancle("Bolnica - Dodaj", bolnicaNewPanel);
            if (n == 0) {
                try {
                    bolnicaBLL.save(bolnicaNewPanel.getBolnica());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionLijecnikDodaj() {
        lijecnikNewPanel.reset(bolnicaBLL.loadAll());
        while (true) {
            int n = prikaziSaveCancle("Lijecnik - Dodaj", lijecnikNewPanel);
            if (n == 0) {
                try {
                    lijecnikBLL.save(lijecnikNewPanel.getLijecnik());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionBolestDodaj() {
        bolestNewPanel.reset();
        while (true) {
            int n = prikaziSaveCancle("Bolest - Dodaj", bolestNewPanel);
            if (n == 0) {
                try {
                    bolestBLL.save(bolestNewPanel.getBolest());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionPacijentDodaj() {
        pacijentNewPanel.reset(lijecnikBLL.loadAll());
        while (true) {
            int n = prikaziSaveCancle("Pacijent - Dodaj", pacijentNewPanel);
            if (n == 0) {
                try {
                    pacijentBLL.save(pacijentNewPanel.getPacijent());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionDijagnozaDodaj() {
        dijagnozaNewPanel.reset(pacijentBLL.loadAll(), bolestBLL.loadAll());
        while (true) {
            int n = prikaziSaveCancle("Dijagnoza - Dodaj", dijagnozaNewPanel);
            if (n == 0) {
                try {
                    dijagnozaBLL.save(dijagnozaNewPanel.getDijagnoza());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionBolnicaPrikazi() {
        Bolnica bolnica = (Bolnica) jList1.getSelectedValue();
        if (bolnica == null) return;
        bolnicaNewPanel.setBolnica(bolnica);
        
        while (true) {
            int n = prikaziSaveCancle("Bolnica - Prikaži", bolnicaNewPanel);
            if (n == 0) {
                try {
                    bolnicaBLL.update(bolnicaNewPanel.getBolnica());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }
    
    private void actionLijecnikPrikazi() {
        Lijecnik lijecnik = (Lijecnik) jList2.getSelectedValue();
        if (lijecnik == null) return;
        lijecnikNewPanel.setLijecnik(lijecnik, bolnicaBLL.loadAll());
        
        while (true) {
            int n = prikaziSaveCancle("Lijecnik - Prikaži", lijecnikNewPanel);
            if (n == 0) {
                try {
                    lijecnikBLL.update(lijecnikNewPanel.getLijecnik());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }
    
    private void actionPacijentPrikazi() {
        Pacijent pacijent = (Pacijent) jList3.getSelectedValue();
        if (pacijent == null) return;
        pacijentNewPanel.setPacijent(pacijent, lijecnikBLL.loadAll());
        
        while (true) {
            int n = prikaziSaveCancle("Pacijent - Prikaži", pacijentNewPanel);
            if (n == 0) {
                try {
                    pacijentBLL.update(pacijentNewPanel.getPacijent());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }
    
    private void actionDijagnozaPrikazi() {
        Dijagnoza dijagnoza = (Dijagnoza) jList4.getSelectedValue();
        if (dijagnoza == null) return;
        dijagnozaNewPanel.setDijagnoza(dijagnoza, pacijentBLL.loadAll(), bolestBLL.loadAll());
        
        while (true) {
            int n = prikaziSaveCancle("Dijagnoza - Prikaži", dijagnozaNewPanel);
            if (n == 0) {
                try {
                    dijagnozaBLL.update(dijagnozaNewPanel.getDijagnoza());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }
    
    private void actionBolestPrikazi() {
        Bolest bolest = (Bolest) jList5.getSelectedValue();
        if (bolest == null) return;
        bolestNewPanel.setBolest(bolest);
        
        while (true) {
            int n = prikaziSaveCancle("Bolest - Prikaži", bolestNewPanel);
            if (n == 0) {
                try {
                    bolestBLL.update(bolestNewPanel.getBolest());
                    break;
                } catch(Exception ex) {
                    prikaziGresku(ex.getMessage());
                }
            } else {
                break;
            }
        }
        loadAll();
    }

    private void actionDijagnozaPretrazi() {
        dijagnozaSearchPanel.reset(pacijentBLL.loadAll(), bolestBLL.loadAll());
        prikaziOk("Dijagnoza - Pretraga", dijagnozaSearchPanel);
    }

}
